package com.supreme.threadserver.threadpkg;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class AtomicTask implements Callable<Long> {

    private final int loopCount;
    private final AtomicLong totalCount;

    public AtomicTask(int loopCount, AtomicLong totalCount) {
        this.loopCount = loopCount;
        this.totalCount = totalCount;
    }

    @Override
    public Long call() throws Exception {
        String threadName = Thread.currentThread().getName();
        for (int i = 0; i < this.loopCount; i++) {
            System.out.printf("Thread: %s count: %d\n", threadName, (i + 1));
            this.totalCount.incrementAndGet();
            TimeUnit.MILLISECONDS.sleep(200);
        }
        return this.totalCount.get();
    }
}
